//
//  UserAccount.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/9/27.
//

import UIKit

@objcMembers
class UserAccount: NSObject, NSCoding {
    // MARK: - 属性
    // 授权AccessToken
    var access_token : String?
    // 过期时间 -> 秒
    var expires_in : TimeInterval = 0.0 {
        // 监听expires_in赋值
        didSet {
            expires_date = Date(timeIntervalSinceNow: expires_in)
        }
    }
    // 用户ID
    var uid : String?
    
    // 过期日期
    var expires_date : Date?
    
    // 昵称
    var screen_name : String?
    
    // 用户的头像地址
    var avatar_large : String?
    
    // MARK: - 自定义构造函数
    init(dict : [String : Any]) {
        super.init()
        setValuesForKeys(dict)
    }
    
    override func setValue(_ value: Any?, forUndefinedKey key: String) {}
    
    // MARK: - 重写description属性
    override var description: String {
        return dictionaryWithValues(forKeys: ["access_token", "uid", "expires_date", "screen_name", "avatar_large"]).description
    }
    
    // MARK: - 归档解档
    // 归档方法
    func encode(with coder: NSCoder) {
        coder.encode(access_token, forKey: "access_token")
        coder.encode(uid, forKey: "uid")
        coder.encode(expires_date, forKey: "expires_date")
        coder.encode(avatar_large, forKey: "avatar_large")
        coder.encode(screen_name, forKey: "screen_name")
    }
    
    // 解档方法
    required init?(coder: NSCoder) {
        access_token = coder.decodeObject(forKey: "access_token") as? String
        uid = coder.decodeObject(forKey: "uid") as? String
        expires_date = coder.decodeObject(forKey: "expires_date") as? Date
        avatar_large = coder.decodeObject(forKey: "avatar_large") as? String
        screen_name = coder.decodeObject(forKey: "screen_name") as? String
    }
    
}
